import 'package:flutter/material.dart';
import 'package:flutter_chubao/config/app_manger_user.dart';
import 'package:flutter_chubao/pages/login/app_login/app_login_view.dart';
import 'package:flutter_chubao/pages/splash_page.dart';
import 'package:flutter_chubao/tools/app_image_load.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'app_main_logic.dart';
import 'app_main_state.dart';

/// @description:
/// @author
/// @date: 2023-12-13 10:16:48
class AppMainPage extends StatelessWidget {
  final AppMainLogic logic = Get.put(AppMainLogic());
  final AppMainState state = Get.find<AppMainLogic>().state;

  AppMainPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AppMainLogic>(
      builder: (controller) {
        return FutureBuilder(
          future: state.netFuture,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              if (state.isNet == false) {
                return Scaffold(
                    backgroundColor: Colors.white,
                    body: Container(
                      color: Colors.white,
                      child: Center(
                          child: GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () async {
                          logic.getNetState(showToast: true);
                        },
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            AppImage.getIconByPath('image_net_fail',
                                width: 120.w),
                            SizedBox(
                              height: 10.w,
                            ),
                            const Text(
                              '网络错误,请检查网络',
                              // style: JFUtilTextStyle.text14White
                              //     .copyWith(color: JFUtilColors.colors333333),
                            ),
                          ],
                        ),
                      )),
                    ));
              } else {
                // return SplashPage();
                return AppManagerUser.isLogin() ? _buildMian() : AppLoginPage();
                // return AppLoginPage();
              }
            } else {
              return Container(
                color: Colors.white,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    AppImage.getIconByPath('icon_app_logo', width: 90.w)
                  ],
                ),
              );
            }
          },
        );
      },
    );
  }

  Widget _buildMian() {
    return Scaffold(
      bottomNavigationBar: BottomAppBar(
        shadowColor: Colors.white,
        // color: Colors.white,
        shape: const AutomaticNotchedShape(
            RoundedRectangleBorder(), StadiumBorder(side: BorderSide())),
        child: SizedBox(
          height: 64,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              InkWell(
                borderRadius: BorderRadius.circular(32),
                onTap: () {
                  logic.clickPage(0);
                },
                child: Container(
                  width: 120,
                  height: 64,
                  alignment: Alignment.center,
                  child: AppImage.getIconByPath(
                      state.tabIndex == 0 ? 'tabbar_home_s' : 'tabbar_home',
                      height: 24),
                ),
              ),
              InkWell(
                  borderRadius: BorderRadius.circular(32),
                  onTap: () {
                    logic.clickPage(1);
                  },
                  child: Container(
                    width: 120,
                    height: 64,
                    alignment: Alignment.center,
                    child: AppImage.getIconByPath(
                        state.tabIndex == 1 ? 'tabbar_mine_s' : 'tabbar_mine',
                        height: 24),
                  ))
            ],
          ),
        ),
      ),
      body: state.tabPage[state.tabIndex],
    );
  }
}
